// pages/index/enrollmentplan/enrollmentplan.js

import {
    GetZsjhlist, // 招生计划
    GetXklist, // 选科
    GetPclist, // 批次
    GetStudentgrades, // 查询我的成绩
} from '../../../api/index'
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp()


Page({

    /**
     * 页面的初始数据
     */
    data: {
        placeholder:'请输入院校名称',
        dataSourceArray: [{
                dictValue: "0",
                dictLabel: "按院校查"
            },
            {
                dictValue: "1",
                dictLabel: "按专业查"
            }
        ],
        option5: [
            {
                text: '2024',
                value: 0
            }
        ],
        value5: 0,
        option1: [
            {
                text: '历史',
                value: 0
            },
            {
                text: '物理',
                value: 1
            },
            {
                text: '艺术(不分科目类)',
                value: 2
            },
            {
                text: '体育(历史类)',
                value: 3
            },
            {
                text: '体育(物理类)',
                value: 4
            }
        ],
        value1: 1,
        choose1:'物理类',
        option2: [{text: '全部',value: 0}, {text: '北京',value: 1},
            {
                text: '天津',
                value: 2
            },
            {
                text: '河北',
                value: 3
            },
            {
                text: '山西',
                value: 4
            },
            {text: '内蒙古',value: 5},
            {text: '辽宁',value: 6},
            {text: '吉林',value: 7},
            {text: '黑龙江',value: 8},
            {text: '上海',value: 9},
            {text: '江苏',value: 10},
            {text: '浙江',value: 11},
            {text: '安徽',value: 12},
            {text: '福建',value: 13},
            {text: '江西',value: 14},
            {text: '山东',value: 15},
            {text: '河南',value: 16},
            {text: '湖北',value: 17},
            {text: '湖南',value: 18},
            {text: '广东',value: 19},
            {text: '广西',value: 20},
            {text: '海南',value: 21},
            {text: '重庆',value: 22},
            {text: '四川',value: 23},
            {text: '贵州',value: 24},
            {text: '云南',value: 25},
            {text: '西藏',value: 26},
            {text: '陕西',value: 27},
            {text: '甘肃',value: 28},
            {text: '青海',value: 29},
            {text: '宁夏',value: 30},
            {text: '新疆',value: 31},
            {text: '香港',value: 32},
            {text: '澳门',value: 33}
        ],
        value2: 0,
        choose2:'',
        title2:"省份",
        option3: [],
        value3: '',
        choose3:'',
        title3:"批次",
        option4: [],
        value4: 9999,
        choose4:'',
        title4:'选科',
        searchString:'',
        tabSelect:0,
        listArray: [],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
        openVip:0
    },

     /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            openVip:app.globalData.openVip
        })
        console.log('openvip=' +  this.data.openVip);
    },
    // 开通vip
    openVipAction() {
        wx.navigateTo({
          url: '/pages/mine/openvip/openvip',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            listArray: [],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
        })
        
        let openid = wx.getStorageSync('openId') || '';
        console.log('openid==' + openid)
        if (openid != '') {
            this._GetStudentgrades('?openId=' + openid, true)
        } else {
            console.log(1233)
            this._GetZsjhlist("?pageNum=" + this.data.pageNo + '&pageSize=10' + '&klmc=' + this.data.choose1 + "&prov=" + this.data.choose2 + "&pcdm=" + this.data.choose3 +"&zyzksyqjc=" + this.data.choose4 + "&yxmc=" + (this.data.tabSelect == 0 ? this.data.searchString : '') + '&zydhmc=' + (this.data.tabSelect == 1 ? this.data.searchString : ''), true);
        }
    },

    inputEvent(event) {
        console.log(event)
        this.setData({
            searchString:event.detail.value
        })
    },

    // 跳转院校详情
    schoolSelectAction(event) {
        if (event.currentTarget.dataset.id == null) {
            return
        }
        wx.navigateTo({
            url: '/pages/index/schooldetail/schooldetail?id=' + event.currentTarget.dataset.id,
        })
    },

    onChange(event) {
        console.log(event);
        if (event.detail.index == 0) {
            this.setData({
                placeholder:'请输入院校名称'
            })
        } else {
            this.setData({
                placeholder:'请输入专业名称'
            })
        }
        this.setData({
            tabSelect:event.detail.index
        })
        this.shuaxin()
    },

    // 搜索点击事件
    handleSearch: function(e) {
        // e.detail.value 包含输入框的当前值
        // var searchValue = e.detail.value;
        // // 在这里执行搜索逻辑
        // console.log('用户点击了搜索，输入的内容为：' + searchValue);
        // this.setData({
        //     searchString: searchValue // 更新对应的data中的值
        // });
        this.shuaxin()
      },


    changeOneValue(event) {
        console.log(event)
        let choose1 = ''
        if (event.detail == 0) {
            choose1 = "历史类"
        } else if (event.detail == 1){
            choose1 = "物理类"
        } else if (event.detail == 2){
            choose1 = "艺术(不分科目类)"
        } else if (event.detail == 3){
            choose1 = "体育(历史类)"
        } else if (event.detail == 4){
            choose1 = "体育(物理类)"
        }
        this.setData({
            choose1:choose1
        })
        this._GetXklist("?klmc=" + this.data.choose1, false);
        this._GetPclist("?klmc=" + this.data.choose1, false);
        this.shuaxin()
    },
    changeTwoValue(event) {
        console.log(event)
        let that = this;
        this.data.option2.forEach(function (child, i) {
            if (event.detail == child.value) {
                if (event.detail == 0) {
                    that.setData({
                        choose2:'',
                        title2:'省份'
                    })
                } else {
                    that.setData({
                        choose2:child.text,
                        title2:''
                    })
                }
            }
        })
        this.shuaxin()
    },

    changeThreeValue(event) {
        console.log(event)
        let that = this;
        this.data.option3.forEach(function (child, i) {
            console.log(child)
            if (event.detail == 9999) {
                that.setData({
                    choose3:'',
                    title3:'批次'
                })
            } else {
                if (event.detail == child.value) {
                        that.setData({
                            choose3:child.value,
                            title3:''
                        })
                }
            }
            
        })
        this.shuaxin()
    },
    changeFourValue(event) {
        console.log(event)
        let that = this;
        if (event.detail == 9999) {
            that.setData({
                choose4:'',
                title4:'选科'
            })
        } else {
            this.data.option4.forEach(function (child, i) {
                if (event.detail == child.value) {
                    that.setData({
                        choose4:child.text,
                        title4:''
                    })
                }
            })
        }
        
        
        this.shuaxin()
    },

    shuaxin () {
        this.setData({
            listArray: [],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
        })
        this._GetZsjhlist("?pageNum=" + this.data.pageNo + '&pageSize=10' + '&klmc=' + this.data.choose1 + "&prov=" + this.data.choose2 + "&pcdm=" + this.data.choose3 +"&zyzksyqjc=" + this.data.choose4 + "&yxmc=" + (this.data.tabSelect == 0 ? this.data.searchString : '') + '&zydhmc=' + (this.data.tabSelect == 1 ? this.data.searchString : ''), false);
    },

     // 我的成绩查询
     _GetStudentgrades(data, isLoading) {
        GetStudentgrades(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            let that = this;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                if (array.length != 0) {
                    let model = array[0]
                    if (model.isWuli == 1) {  // 物理
                        that.setData({
                            value1:1,
                            choose1:'物理类'
                        })
                    } else { // 历史
                        that.setData({
                            value1:0,
                            choose1:'历史类'
                        })
                    }
                    this._GetXklist("?klmc=" + this.data.choose1, false);
        this._GetPclist("?klmc=" + this.data.choose1, false);
                    that._GetZsjhlist("?pageNum=" + that.data.pageNo + '&pageSize=10' + '&klmc=' + that.data.choose1 + "&prov=" + that.data.choose2 + "&pcdm=" + that.data.choose3 +"&zyzksyqjc=" + that.data.choose4 + "&yxmc=" + (that.data.tabSelect == 0 ? this.data.searchString : '') + '&zydhmc=' + (that.data.tabSelect == 1 ? that.data.searchString : ''), true);
                } else {
                    this._GetXklist("?klmc=" + this.data.choose1, false);
        this._GetPclist("?klmc=" + this.data.choose1, false);
                    that._GetZsjhlist("?pageNum=" + that.data.pageNo + '&pageSize=10' + '&klmc=' + that.data.choose1 + "&prov=" + that.data.choose2 + "&pcdm=" + that.data.choose3 +"&zyzksyqjc=" + that.data.choose4 + "&yxmc=" + (that.data.tabSelect == 0 ? this.data.searchString : '') + '&zydhmc=' + (that.data.tabSelect == 1 ? that.data.searchString : ''), true);
                }
            } else {
                // Toast(res.msg)
            }
        })
    },

    _GetZsjhlist(data, isLoading) {
        console.log('招生计划')
        let that = this
        if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
            this.setData({
                nomore: true
            })
            return;
        }
        GetZsjhlist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.rows;
                that.setData({
                    listArray: that.data.listArray.concat(array),
                    pageNo: that.data.pageNo + 1,
                    totalPages: res.total,
                });

                if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                    that.setData({
                        nomore: true
                    })
                }
            } else {
                Toast(res.msg)
            }
        })
    },


    //  选科
    _GetXklist(data, isLoading) {
        this.setData({
            choose4:'',
            title4:'选科',
        })
        GetXklist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.data
                let option4 = [{text:'全部',value:9999}]
                array.forEach(function (child, i) {
                    option4.push({
                        text: child.zyzksyqjc,
                        value: i
                    })
                });
                this.setData({
                    option4: option4,
                    value4: 9999
                })
            } else {
                Toast(res.msg)
            }
        })
    },

    //  批次
    _GetPclist(data, isLoading) {
        this.setData({
            choose3:'',
        title3:"批次",  
        })
        GetPclist(data, isLoading).then(res => {
            console.log(res)
            let code = res.code;
            // Toast('成功')
            if (code == 200) {
                let array = res.data
                let option3 = [{text:'全部',value:9999}]
                array.forEach(function (child, i) {
                    option3.push({
                        text: child.pcmc,
                        value: child.pcdm,
                    })
                });
                this.setData({
                    option3: option3,
                    value3: option3[0].value
                })
            } else {
                Toast(res.msg)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log('下拉刷新')
        // 显示顶部刷新图标
        wx.showNavigationBarLoading();
        this.setData({
            listArray: [],
            pageNo: 1,
            totalPages: 0,
            nomore: false,
        })
        this._GetZsjhlist("?pageNum=" + this.data.pageNo + '&pageSize=10' + '&klmc=' + this.data.choose1 + "&prov=" + this.data.choose2 + "&pcdm=" + this.data.choose3 +"&zyzksyqjc=" + this.data.choose4 + "&yxmc=" + (this.data.tabSelect == 0 ? this.data.searchString : '') + '&zydhmc=' + (this.data.tabSelect == 1 ? this.data.searchString : ''), false);
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this._GetZsjhlist("?pageNum=" + this.data.pageNo + '&pageSize=10' + '&klmc=' + this.data.choose1 + "&prov=" + this.data.choose2 + "&pcdm=" + this.data.choose3 +"&zyzksyqjc=" + this.data.choose4 + "&yxmc=" + (this.data.tabSelect == 0 ? this.data.searchString : '') + '&zydhmc=' + (this.data.tabSelect == 1 ? this.data.searchString : ''), false);
    },


    onShareTimeline: function (){

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})